Micron Document
🎖️GitЯра🎖️

Node / meshtastic / Meshtastic-Android / files / core / database / src / androidDeviceTest / kotlin / org / meshtastic / core / database / DatabaseManagerLegacyCleanupTest.kt

Displaying Raw • Download

core/database/src/androidDeviceTest/kotlin/org/meshtastic/core/database/DatabaseManagerLegacyCleanupTest.kt 65a1f5ce4d0e4b8eafcd0bbfccafd41dc6ee888a (65a1f5ce) Text, 3.18 KB

T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.core.database

Tff7b72import T7ee787android.app.Application
Tff7b72import T7ee787android.content.Context
Tff7b72import T7ee787androidx.datastore.preferences.core.booleanPreferencesKey
Tff7b72import T7ee787androidx.datastore.preferences.core.edit
Tff7b72import T7ee787androidx.datastore.preferences.preferencesDataStoreFile
Tff7b72import T7ee787androidx.test.core.app.ApplicationProvider
Tff7b72import T7ee787androidx.test.ext.junit.runners.AndroidJUnit4
Tff7b72import T7ee787kotlinx.coroutines.Dispatchers
Tff7b72import T7ee787kotlinx.coroutines.delay
Tff7b72import T7ee787kotlinx.coroutines.runBlocking
Tff7b72import T7ee787org.junit.Assert.assertFalse
Tff7b72import T7ee787org.junit.Assert.assertTrue
Tff7b72import T7ee787org.junit.Test
Tff7b72import T7ee787org.junit.runner.RunWith
Tff7b72import T7ee787org.meshtastic.core.common.ContextServices
Tff7b72import T7ee787org.meshtastic.core.database.di.asDatabaseDataStore
Tff7b72import T7ee787org.meshtastic.core.di.CoroutineDispatchers

Tf0883e@RunWithTb4b4b4(Te6edf3AndroidJUnit4Tff7b72::Te6edf3classTb4b4b4)
Tff7b72class T56d364DatabaseManagerLegacyCleanupTest Tb4b4b4{
Tf0883e@Test
Tff7b72fun Td2a8ffdeletes_legacy_db_on_switch_when_flag_not_setTb4b4b4(Tb4b4b4) Tff7b72= Te6edf3runBlocking Tb4b4b4{
Tff7b72val Te6edf3app Tff7b72= Te6edf3ApplicationProviderTb4b4b4.Te6edf3getApplicationContextTff7b72<Te6edf3ApplicationTff7b72>Tb4b4b4(Tb4b4b4)
Te6edf3ContextServicesTb4b4b4.Te6edf3app Tff7b72= Te6edf3app
Tff7b72val Te6edf3datastoreName Tff7b72= Ta5d6ff"Ta5d6ffdb-manager-prefs-test-Tffd700${Te6edf3SystemTb4b4b4.Te6edf3nanoTimeTb4b4b4(Tb4b4b4)Tffd700}Ta5d6ff"
Tff7b72val Te6edf3datastore Tff7b72= Te6edf3createDatabaseDataStoreTb4b4b4(Te6edf3datastoreNameTb4b4b4)Tb4b4b4.Te6edf3asDatabaseDataStoreTb4b4b4(Tb4b4b4)

T8b949e// Reset the one-time flag
Tff7b72val Te6edf3legacyCleanedKey Tff7b72= Te6edf3booleanPreferencesKeyTb4b4b4(Te6edf3DatabaseConstantsTb4b4b4.Te6edf3LEGACY_DB_CLEANED_KEYTb4b4b4)
Te6edf3datastoreTb4b4b4.Te6edf3edit Tb4b4b4{ Tffa657itTb4b4b4.Te6edf3removeTb4b4b4(Te6edf3legacyCleanedKeyTb4b4b4) Tb4b4b4}

T8b949e// Ensure legacy DB file exists
Tff7b72val Te6edf3legacyName Tff7b72= Te6edf3DatabaseConstantsTb4b4b4.Te6edf3LEGACY_DB_NAME
Tff7b72val Te6edf3legacyFile Tff7b72= Te6edf3appTb4b4b4.Te6edf3getDatabasePathTb4b4b4(Te6edf3legacyNameTb4b4b4)
T8b949e// Create or overwrite the legacy DB file by opening it once
Te6edf3appTb4b4b4.Te6edf3openOrCreateDatabaseTb4b4b4(Te6edf3legacyNameTb4b4b4, Te6edf3ContextTb4b4b4.Te6edf3MODE_PRIVATETb4b4b4, Tff7b72nullTb4b4b4)Tb4b4b4.Te6edf3closeTb4b4b4(Tb4b4b4)
Te6edf3assertTrueTb4b4b4(Ta5d6ff"Ta5d6ffPrecondition: legacy DB should exist before switchTa5d6ff"Tb4b4b4, Te6edf3legacyFileTb4b4b4.Te6edf3existsTb4b4b4(Tb4b4b4)Tb4b4b4)

Tff7b72val Te6edf3testDispatchers Tff7b72=
Te6edf3CoroutineDispatchersTb4b4b4(Te6edf3io Tff7b72= Te6edf3DispatchersTb4b4b4.Te6edf3IOTb4b4b4, Te6edf3main Tff7b72= Te6edf3DispatchersTb4b4b4.Te6edf3MainTb4b4b4, Te6edf3default Tff7b72= Te6edf3DispatchersTb4b4b4.Te6edf3DefaultTb4b4b4)
Tff7b72val Te6edf3manager Tff7b72= Te6edf3DatabaseManagerTb4b4b4(Te6edf3datastoreTb4b4b4, Te6edf3testDispatchersTb4b4b4)

T8b949e// Switch to a non-null address so active DB != legacy
Te6edf3managerTb4b4b4.Te6edf3switchActiveDatabaseTb4b4b4(Ta5d6ff"Ta5d6ff01:23:45:67:89:ABTa5d6ff"Tb4b4b4)

T8b949e// Cleanup runs asynchronously; wait briefly for deletion
Tff7b72var Te6edf3attempts Tff7b72= T79c0ff0
Tff7b72while Tb4b4b4(Te6edf3legacyFileTb4b4b4.Te6edf3existsTb4b4b4(Tb4b4b4) Tff7b72&Tff7b72& Te6edf3attempts Tff7b72< T79c0ff2T79c0ff0Tb4b4b4) Tb4b4b4{
Te6edf3delayTb4b4b4(T79c0ff1T79c0ff0T79c0ff0Tb4b4b4)
Te6edf3attemptsTff7b72+Tff7b72+
Tb4b4b4}

Te6edf3assertFalseTb4b4b4(Ta5d6ff"Ta5d6ffLegacy DB should be deleted after switchTa5d6ff"Tb4b4b4, Te6edf3legacyFileTb4b4b4.Te6edf3existsTb4b4b4(Tb4b4b4)Tb4b4b4)

T8b949e// Clean up
Te6edf3appTb4b4b4.Te6edf3preferencesDataStoreFileTb4b4b4(Te6edf3datastoreNameTb4b4b4)Tb4b4b4.Te6edf3deleteTb4b4b4(Tb4b4b4)
Tb4b4b4}
Tb4b4b4}

Served by rngit 1.5.0 - Generated in 0.07s